package com.edgardeng.publish_subscribe.route;

import com.edgardeng.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.Date;

/**
 * RabbitMQ 测试 Publish/Subscribe  - 发布者发送消息
 *
 */
public class Sender {
    public static void main(String[] args) throws Exception{
        String exchange = ConnectionUtil.EXCHANGE_NAME_DIRECT;
        String routeKey = ConnectionUtil.ROUTER_KEY_DIRECT_ONE;
        String type = ConnectionUtil.EXCHANGE_TYPE_DIRECT;
        System.out.println("[Sender] exchange: " + exchange + "    type: " + type);
        String message = "I'm sending Publish/Subscribe message in route: "+ routeKey + " at " + new Date().toString();

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(exchange, type); // 声明交换机
        channel.basicPublish(exchange, routeKey,null, message.getBytes()); // 发送消息
        System.out.println("[Sender] " + message);
        channel.close();
        connection.close();
    }


}
